//
//  WdxyybVC.swift
//  Auscultation
//
//  Created by 来 on 2020/12/22.
//

import UIKit

class WdxyybVC: BaseVC {
    
    var collectionView: UICollectionView!
    
    var dataArray: [[String: Any]] = []
    var pageNum: Int = 1
    var pageSize: Int = 10
    var keyWord: String = ""
    var labelStatus: String = ""
    var auditStatus: String = ""
    var status: String = ""
    
    var sx_state: [Int] = [0, 0, 0]

    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        collectionView.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(loadData))
        collectionView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
        loadData()
        NotificationCenter.default.addObserver(self, selector: #selector(WdxyybVC.loadData), name: NSNotification.Name(rawValue: "ReloadWdxyybVC"), object: nil)
    }
    
    // 数据刷新
    @objc func loadData() {
        pageNum = 1
        dataArray.removeAll()
        loadMoreData()
    }
    @objc func loadMoreData() {
        let paramet: [String: String] = [
            "keyWord" : keyWord,
            "labelStatus" : labelStatus,
            "auditStatus" : auditStatus,
            "status" : status,
            "pageNo" : pageNum.description,
            "pageSize" : pageSize.description
        ]
        EWERequest.lgf("sample", paramet, .get) { [weak self] (data) in
            let result = (data["result"] as? [[String: Any]]) ?? []
            if result.count > 0 {
                self?.dataArray += result
                self?.pageNum += 1
            }
            self?.collectionView.reloadData()
            self?.endRefreshing()
            if result.count == 0 {
                self?.collectionView.mj_footer?.endRefreshingWithNoMoreData()
            }
        } _: { [weak self] (error) in
            SVProgressHUD.showError(error)
            self?.endRefreshing()
        }
    }
    func endRefreshing() {
        self.collectionView.mj_header?.endRefreshing()
        self.collectionView.mj_footer?.endRefreshing()
    }

    override func rightBtnSelect(_ sender: UIButton) {
        self.navigationController?.show(XzxyybVC(), sender: self)
    }

    @objc func s_sx() {
        SxV().showIn(self.view, ["标注状态", "审核状态", "样本状态"],
                     ["标注状态": ["未开始", "标注开始", "标注完成"],
                      "审核状态": ["未开始", "审核开始", "审核完成"],
                      "样本状态": ["导入开始", "导入完成"]], self.sx_state) { [weak self] (sxv, name, state) in
            
            if state[0] == 0 {
                self?.labelStatus = ""
            } else if state[0] == 1 {
                self?.labelStatus = "0"
            } else if state[0] == 2 {
                self?.labelStatus = "10"
            } else if state[0] == 3 {
                self?.labelStatus = "20"
            }
            if state[1] == 0 {
                self?.auditStatus = ""
            } else if state[1] == 1 {
                self?.auditStatus = "0"
            } else if state[1] == 2 {
                self?.auditStatus = "10"
            } else if state[1] == 3 {
                self?.auditStatus = "20"
            }
            if state[2] == 0 {
                self?.status = ""
            } else if state[2] == 1 {
                self?.status = "10"
            } else if state[2] == 2 {
                self?.status = "20"
            }
            if self?.sx_state[0] != state[0] || self?.sx_state[1] != state[1] || self?.sx_state[2] != state[2] {
                self?.loadData()
            }
            self?.sx_state[0] = state[0]
            self?.sx_state[1] = state[1]
            self?.sx_state[2] = state[2]
        }
    }
    
}

extension WdxyybVC: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        keyWord = textField.text ?? ""
        loadData()
    }
}

extension WdxyybVC : UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "YbCellPlayerStop"), object: nil)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize  {
        return CGSize.init(width: UIDevice.lgf_ScreenW, height: 230)
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.lgf_DequeueReusableCell(with: YbCell.self, for: indexPath)
        cell.tzzx.text = (dataArray[indexPath.item]["no"] as? String) ?? ""
        cell.cjsj.text = "创建时间：" + ((dataArray[indexPath.item]["createdTime"] as? Int) ?? 0).description.lgf_TimeStampToDate().lgf_TimeStr("yyyy-MM-dd HH:mm:ss")
        cell.bzb.text = "标注包：" + ((dataArray[indexPath.item]["labelName"] as? String) ?? "")
        cell.shb.text = "审核包：" + ((dataArray[indexPath.item]["auditName"] as? String) ?? "")
        cell.url = ((dataArray[indexPath.item]["sampleDetail"] as? [String: Any])?["wavPath"] as? String) ?? ""
        cell.initPlay()
        cell.allTime = cell.player.getAllTime(((dataArray[indexPath.item]["sampleDetail"] as? [String: Any])?["duration"] as? Int) ?? 0)
        cell.play_time.text = "00:00/" + cell.allTime
        let deleted = (dataArray[indexPath.item]["isDeleted"] as? Int) ?? 0
        if deleted == 0 {
            cell.deleted.text = "作废"
            cell.deleted.backgroundColor = UIColor.init(lgf_HexString: "CE4C4C")?.withAlphaComponent(0.1)
            cell.deleted.textColor = UIColor.init(lgf_HexString: "CE4C4C")
            cell.deleted.layer.borderColor = UIColor.init(lgf_HexString: "CE4C4C")?.withAlphaComponent(0.2).cgColor
        } else {
            cell.deleted.text = "有效"
            cell.deleted.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")?.withAlphaComponent(0.1)
            cell.deleted.textColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.deleted.layer.borderColor = UIColor.init(lgf_HexString: "0F88F2")?.withAlphaComponent(0.2).cgColor
        }
        let status = (dataArray[indexPath.item]["status"] as? Int) ?? 0
        cell.status_v = status
        if status == 10 {
            cell.status.text = "导入开始"
        } else if status == 20 {
            cell.status.text = "导入完成"
        }
        let workflow = (dataArray[indexPath.item]["workflow"] as? Int) ?? 0
        if workflow == 0 {
            cell.bzks_i.isHidden = true
            cell.bzwc_i.isHidden = true
            cell.shks_i.isHidden = true
            cell.shwc_i.isHidden = true
            cell.bzks.textColor = UIColor.init(lgf_HexString: "D0E3F6")
            cell.bzwc.textColor = UIColor.init(lgf_HexString: "D0E3F6")
            cell.shks.textColor = UIColor.init(lgf_HexString: "D0E3F6")
            cell.shwc.textColor = UIColor.init(lgf_HexString: "D0E3F6")
            cell.lc_line1.backgroundColor = UIColor.init(lgf_HexString: "D0E3F6")
            cell.lc_line2.backgroundColor = UIColor.init(lgf_HexString: "D0E3F6")
            cell.lc_line3.backgroundColor = UIColor.init(lgf_HexString: "D0E3F6")
        } else if workflow == 10 {
            cell.bzks_i.isHidden = false
            cell.bzwc_i.isHidden = true
            cell.shks_i.isHidden = true
            cell.shwc_i.isHidden = true
            cell.bzks.textColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.bzwc.textColor = UIColor.init(lgf_HexString: "D0E3F6")
            cell.shks.textColor = UIColor.init(lgf_HexString: "D0E3F6")
            cell.shwc.textColor = UIColor.init(lgf_HexString: "D0E3F6")
            cell.lc_line1.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.lc_line2.backgroundColor = UIColor.init(lgf_HexString: "D0E3F6")
            cell.lc_line3.backgroundColor = UIColor.init(lgf_HexString: "D0E3F6")
        } else if workflow == 15 {
            cell.bzks_i.isHidden = false
            cell.bzwc_i.isHidden = false
            cell.shks_i.isHidden = true
            cell.shwc_i.isHidden = true
            cell.bzks.textColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.bzwc.textColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.shks.textColor = UIColor.init(lgf_HexString: "D0E3F6")
            cell.shwc.textColor = UIColor.init(lgf_HexString: "D0E3F6")
            cell.lc_line1.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.lc_line2.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.lc_line3.backgroundColor = UIColor.init(lgf_HexString: "D0E3F6")
        } else if workflow == 20 {
            cell.bzks_i.isHidden = false
            cell.bzwc_i.isHidden = false
            cell.shks_i.isHidden = false
            cell.shwc_i.isHidden = true
            cell.bzks.textColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.bzwc.textColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.shks.textColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.shwc.textColor = UIColor.init(lgf_HexString: "D0E3F6")
            cell.lc_line1.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.lc_line2.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.lc_line3.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
        } else if workflow == 25 || workflow == 30 {
            cell.bzks_i.isHidden = false
            cell.bzwc_i.isHidden = false
            cell.shks_i.isHidden = false
            cell.shwc_i.isHidden = false
            cell.bzks.textColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.bzwc.textColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.shks.textColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.shwc.textColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.lc_line1.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.lc_line2.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
            cell.lc_line3.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
        }
        return cell
    }
}
